import React from 'react';
import { getPermissions } from '../utils/getPermissions';

export const withPermission = WrappedComponent => class extends React.Component {
	state = {
		isContains: false
	}
	componentDidMount() {
		this.receivePermissions()
	}
	receivePermissions = async () => {
		let permissions = await getPermissions()
		const { actionCode } = this.props;
		this.setState({
			isContains: actionCode ? permissions.includes(actionCode) : true
		})
	}
	render() {
		const { isContains } = this.state;
		return (
			isContains
				? <WrappedComponent {...this.props} />
				: null
		)
	}
}